﻿/*
* 作 者: 槐尧
* 公 司: 墨宇天城
* 创建时间: 2016/3/4 10:55:39
* 说明:  
*/

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;

public class FruitDataManager
{
    private static FruitDataManager intstance;
    public static FruitDataManager Intstance
    {
        get
        {
            if (intstance == null)
            {
                intstance = new FruitDataManager();
                intstance.Init();
            }
                
            return intstance;
        }
    }

    private static int m_UserCoin = 0;

    public static int UserCoin
    {
        get { return FruitDataManager.m_UserCoin; }
        set { FruitDataManager.m_UserCoin = value; }
    }

    const string dataPath = "fruitdata";
    private Dictionary<FruitEnum, FruitGiven> m_fruitGiven = new Dictionary<FruitEnum, FruitGiven>();
    private Dictionary<int, FruitData> m_fruitData = new Dictionary<int, FruitData>();
    private Dictionary<int, int> m_defaultPrice = new Dictionary<int, int>();
    private List<int> m_recordFruitprecent = new List<int>();
    private List<FruitData> fdlist = new List<FruitData>();
    private int CalculateNum;
    public void Init()
    {
        InitDefault();
    }

    /// <summary>
    /// 初始化默认数据
    /// </summary>
    public void InitDefault()
    {
        string content = PlayerPrefs.GetString("dataPath", "");
        if (content == "")
        {
            content = Resources.Load<TextAsset>("txt/fruitdata").text;
        }
      //  InitDate(content);
    }

    /// <summary>
    /// 根据内容初始化数据
    /// </summary>
    /// <param name="content"></param>
    public void InitDate(string content)
    {
        CalculateNum = 0;
        m_fruitGiven.Clear();
        m_fruitData.Clear();
        m_defaultPrice.Clear();
        m_recordFruitprecent.Clear();
        fdlist.Clear();
        Dictionary<string, object> dataDic = Json.Deserialize(content) as Dictionary<string, object>;
        List<object> lstFruitGiven = dataDic["givedata"] as List<object>;
        foreach (Dictionary<string, object> o in lstFruitGiven)
        {
            FruitGiven fg = new FruitGiven(o);
            m_fruitGiven[fg.iFruitid] = fg;
        }

        List<object> lstFruitData = dataDic["fruitdata"] as List<object>;
        foreach (Dictionary<string, object> o in lstFruitData)
        {
            FruitData fd = new FruitData(o);
            m_fruitData[fd.iIndex] = fd;
            if (fd.iNum != 0)
            {
                m_defaultPrice[fd.iIndex] = fd.iNum;
            }
            else
            {
                if (fd.iFruitId == FruitEnum.GoodLuck1)
                {
                    m_defaultPrice[fd.iIndex] = 101;
                }
                else if (fd.iFruitId == FruitEnum.GoodLuck2)
                {
                    m_defaultPrice[fd.iIndex] = 102;
                }
                else
                {
                    m_defaultPrice[fd.iIndex] = m_fruitGiven[fd.iFruitId].iGivenNum;
                }
            }
            fdlist.Add(fd);
        }
        List<object> fruitprecent = dataDic["fruitprecent"] as List<object>;
        foreach (int o in fruitprecent)
        {
            CalculateNum += o;
            m_recordFruitprecent.Add(CalculateNum);
        }
    }
    public List<FruitData>  GetFruitDatalist()
    {
        return fdlist;
    }
    public FruitData GetFruitData(int index)
    {
        return m_fruitData[index];
    }
    /// <summary>
    /// 获取defaultPrice
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public int GetdefaultPrice(int index)
    {
        return m_defaultPrice[index];
    }
    /// <summary>
    /// 获取概率总数
    /// </summary>
    /// <returns></returns>
    public int GetCalculateNum()
    {
        return CalculateNum;
    }
    /// <summary>
    /// 记录24个物品的概率
    /// </summary>
    /// <returns></returns>
    public List<int> GetrecordFruitprecent()
    {
        return m_recordFruitprecent;
    }
    /// <summary>
    /// 获取每种物品的赔率
    /// </summary>
    /// <param name="fruitId"></param>
    /// <returns></returns>
    public int Getprobability(int fruitId)
    {
        return m_fruitGiven[(FruitEnum)fruitId].iGivenNum;
    }
    /// <summary>
    /// 加载默认数据
    /// </summary>
    private void LoadDefaultData()
    {

    }

    public void SaveConfigData()
    {

    }
}